import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container-scroll'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const UploadFileTable		= $Lazy.state(()=>import('./cpt/upload-file-table'))
// ===================================================================== Lazy
const width = '25%'
const boolData = [{ name:'是', value: '1' }, { name:'否', value:'0' }]
const statusData = [{ name:'草稿', value: '0' }, { name:'正常', value:'1' }, { name:'已过期', value:'2' }]
const typeData = [{ name:'预备合同', value: '1' }, { name:'正式合同', value:'2' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label: '合同编号',				name: 'code', 				width, required:true },
			{ label: '合同名称',				name: 'name', 				width, required:true },
			{ label: '客户名称',				name: 'hosp_id', 			width, required:true, type:'select' },
			{ label: '合同折扣率(小数点)',	name: 'discount_rate', 		width, required:true },
			{ label: '结算周期',				name: 'reckon_day', 		width, 		type:'select', loading:true, required:true },
			{ label: '回款周期',				name: 'back_day', 			width, 		type:'select', loading:true, required:true },
			{ label: '对票',					name: 'right_vote', 		width,		type:'radio', data:boolData, required:true },
			{ label: '信用期',				name: 'credit', 			width, 		type:'number' },
			{ label: '合并开票',				name: 'merge_vote', 		width,		type:'radio', data:boolData, required:true },
			{ label: '状态',					name: 'status', 			width, 		type:'select', data: statusData, required:true },
			{ label: '生效时间',			name: 'sign_at', 			width,  	required:true, 	type:'date-time', onValue:({ value, formData })=>{
				$fn.getRow(formData,'contract_validity').before = value
				this.setState({ formData })
			}},
			{ label: '到期时间',			name: 'contract_validity', 	width, 		required:true, 	type:'date-time', onValue:({ value, formData })=>{
				$fn.getRow(formData,'sign_at').after = value
				this.setState({ formData })
			}},
			{ label: '延期时间',				name: 'delay', 				width, 		type:'number',  afterText:'月', required:true},
			{ label: '合同类型',				name: 'type', 				width:'75%',		type:'radio', data:typeData, required:true },
			{ type:'cpt', component: ()=> <UploadFileTable uuid={this.uuid} onRef={ ref => this.uploadFileTable = ref } disabled={!this.state.edit} />, full:true },
			{ label: '合同备注',				name: 'remark', 			full:true,  type:'textarea' },
			{ label: '其他开票特殊要求',		name: 'special', 			full:true,  type:'textarea' },
			{ label: '结算单打印要求',		name: 'printing', 			full:true,  type:'textarea' },
		],
	}
	
	componentDidMount(){
		const { uuid, edit } = $fn.query()
		this.uuid = uuid
		const { formData } = this.state
		 // 编辑和添加隐藏status 2022-4-21 --马婷 
		if(+edit === 1 || +edit === 2){
			$fn.hideRows(formData,['status'], true)
		}else{
			$fn.hideRows(formData,['status'], false)
		}
		this.setState({ edit, formData })
		
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 客户名称
			$fn.setRow(formData, 'hosp_id', await f.getCache({ api:'hospital' }))
			// 结算周期
			$fn.setRow(formData, 'reckon_day', await f.getDisCache({ code: 44300 }))
			// 回款周期
			$fn.setRow(formData, 'back_day', await f.getDisCache({ code: 44400 }))
			//
			this.setState({ formData })
		})
	}
	
	fetch = () => {
		if(this.uuid){
			$http.submit(this, 'bs-contract/info', { param:{ uuid:this.uuid } }).then( async data=>{
				const { uuid, sign_at, contract_validity } = data
				
				const { formData } = this.state
				$fn.getRow(formData, 'sign_at')['after'] = contract_validity
				$fn.getRow(formData, 'contract_validity')['before'] = sign_at
			
				this.submit = { uuid }
				
				this.formRef.setValue( data )
			})
		}
	}
	
	// 操作按钮
	controls = () => [
		this.state.edit && Controls.click.call(this,'保存', ()=> this.formRef.submit(), { code:'Enter'} ),
		Controls.back.call(this)
	]
	
	render(){
		const { edit } = this.state
		let txt = '查看'
		let api = 'bs-contract/edit'
		if(+edit === 1){
			txt = '编辑'
		}else if( +edit === 2){
			txt = '添加'
			api = 'bs-contract/add'
		}
		return (
			<Page title={`合同管理${txt}`} className='p10' controls={this.controls()}>
				{ SubmitForm.call(this,{ labelWidth:'s11', msg:'保存', api, disabled: !edit, onForm: this.fetch, backRefresh:true }) }
			</Page>
		)
	}
}

export default Index